var loginPara = {}; //用户登陆参数


//判断字符串是否为空
function isNotEmpty(str){
    if(str==undefined||str==null||str==''){
        return false;
    }else{
        return true;
    }
}
//判断用户是否登陆
function isLogin(){
    var userId = $.cookie('userId');
    if(isNotEmpty(userId)){
        loginPara.userId = userId;
        loginPara.language = $.cookie('language');
        loginPara.loginCode = $.cookie('loginName');
        $('#uname').text($.cookie('loginName'));
        return true;
    }else{
        return false;
    }
    
}

//初始化主菜单
function initTopMenu(pmid,mid){
    if(isLogin()){
        var url = centerRUI+"/sysUser/getCloudOMenuByUserId";
        $.ajax({
            url :url,
            type:"get",
            data:{
                'language':loginPara.language,
                'userId':loginPara.userId,
                'loginCode':loginPara.loginCode,
                'sysModId':pmid
            },
            dataType:'json',
            success:function(resp){
                if(resp!=''&&resp!=undefined&&resp.length) {
                    var $ul = $('#minMenu>ul');
                    for (var i = 0; i < resp.length; i++) {
                        var $li = $('<li>');
                        $li.attr('id', resp[i].id);
                        if (resp[i].id == mid) {
                            $li.addClass('checkMinMenu');
                        }
                        var $p1 = $('<p>');
                        var $img = $('<img>');
                        switch (resp[i].id) {
                            case '360':
                                $li.bind('click',function(){
                                    window.location.href=myurl+'/index.jsp';
                                });
                                $img.attr('src', 'img/logo/header01.png');
                                break;
                            case '361':
                                $li.bind('click',function(){
                                    window.location.href=myurl+'/index.jsp';
                                });

                                $img.attr('src', 'img/logo/header02.png');
                                break;
                            case '362':
                                $li.bind('click',function(){
                                    window.location.href=myurl+'/params_set.jsp';
                                });
                                $img.attr('src', 'img/logo/header03.png');
                                break;
                            case '363':
                                $li.bind('click',function(){
                                    window.location.href=myurl+'/index.jsp';
                                });
                                $img.attr('src','img/logo/header04.png');
                                break;
                            default:
                                break;
                        }

                        $p1.append($img);
                        var $p2 = $('<p>');
                        $p2.text(resp[i].text);
                        $li.append($p1);
                        $li.append($p2);
                        $ul.append($li);
                    }
                    if(mid=='360'){
                    	if(loginPara.loginCode!="admin"){
                    		checkPermission(mid)
                    	}
                    }else if(mid=='362'){
                    	initLeftMenu(pmid, 362);
                    }
//                    initLeftMenu(pmid, mid);
                }else{
                    console.log('主菜单加载数据为空，请检查控制中心');
                }
            },
            error:function(){
                alert('与服务器通讯错误');
            }
        });

    }

}
//初始化左侧菜单
function initLeftMenu(pmid,mid){
    var url = centerRUI + "/sysUser/getCloudSMenuByUserId";
//    http://192.168.1.130:8080/centerweb/cloud/sysUser/getCloudSMenuByUserId?language=zh-CN&userId=1&loginCode=8080&sysModId=359&sysModSId=362
    $.ajax({
        url: url,
        type: "get",
        data: {
            'language': loginPara.language,
            'userId': loginPara.userId,
            'loginCode': loginPara.loginCode,
            'sysModId': pmid,
            'sysModSId': mid
        },
        dataType: 'json',
//        beforeSend: function () {
//            $('#accordion').html('加载中...');
//        },
        success: function (resp) {
//            resp.mid = mid;
//            resp.smid = smid;
			 for (var i = 0; i < resp.length; i++) {
						switch (resp[i].id) {
						case '608':
							var html = "<li role='tablink' class='active'><a href='#personal-params' data-toggle='tab'>个人参数</a></li>"
							break;
						case '609':
							html = "<li role='tablink'><a href='#public-params' data-toggle='tab'>公共参数</a></li>"
							break;
						case '610':
							html = "<li role='tablink'><a href='#mediadv-state-describe' data-toggle='tab'>全局参数</a></li>"
							break;
						default:
							break;
						}
						$("#params_leftmenu").append(html);
					}
//            laytplUrl(host + "/module/common/menu.html?_=" + new Date(), resp, function (data, html) {
//                $('#accordion').html(html);
//            });
        },
        error: function () {
            alert('与服务器通讯错误');
        }
    });
}
//菜单或按钮权限检查
function checkPermission(mid){
    if(mid!=undefined&&mid!='') {
        var url = centerRUI + "/sysUser/initCloudUser";
//        http://192.168.1.130:8080/centerweb/cloud/sysUser/initCloudUser?modId=360&userId=1&loginCode=8080
        $.ajax({
            url: url,
            type: "get",
            data: {
                'userId': loginPara.userId,
                'loginCode': loginPara.loginCode,
                'modId': mid
            },
            dataType: 'json',
            success: function (resp) {
            
                if (resp != null && resp != undefined && resp.length) {
                	var allbtn=$("#change-content").find("button");
                	for(var i=0;i<resp.length;i++){
                		for (var j=0;j<allbtn.length;j++){
                    		if(allbtn[j].name==resp[i].btnCode){
                    			$(allbtn[j]).show();
                    		}else{
                    			$(allbtn[j]).hide();
                    		}
                    	}
                	}
                	
//                    switch(mid){
//                        case 414: window.location.href=host+"/jsp/index.jsp";break;
//                        case 415: window.location.href=host+"/jsp/main/mypatients.jsp";break;
//                        case 416: window.location.href=host+"/jsp/main/cooperpatients.jsp";break;
//                        case 417: window.location.href=host+"/jsp/main/bedoverview.jsp";break;
//                        default:break;
//                    }
                } else {
                    alert('您无此功能权限！');
                }
            },
            error: function () {
                alert('与服务器通讯错误');
            }
        });
    }
}
//初始化子菜单链接
function initSubmenu(pinfo){
    //临床路径
    var VCN01 = pinfo.vcn01==undefined?0:pinfo.vcn01;
    var clurl = $('#clipath').attr('href');
    clurl+='?VCN01='+VCN01;
    $('#clipath').attr('href',clurl)
    //临床路径概要
    var clurl2 = $('#clipath2').attr('href');
    clurl2+='?VCN01='+VCN01;
    $('#clipath2').attr('href',clurl2)
    //护理路径概要
    var clurl3 = $('#clipath3').attr('href');
    clurl3+='?VCN01='+VCN01;
    $('#clipath3').attr('href',clurl3)
}






